package com.wchen.gdoc.generate.util;

import com.alibaba.fastjson.JSONObject;

import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * sql转换器处理
 *
 * @author  wchen
 * @date    2019/11/8
 */
public class StringTemplateUtil {
    private final static Pattern PATTERN = Pattern.compile("\\$\\{[a-zA-Z]+\\}");

    /**
     * 动态替换模板内容
     *
     * @param configSql
     * @param replaceParam
     * @return
     */
    public static String buildSql(String configSql, JSONObject replaceParam){
        Matcher matcher = PATTERN.matcher(configSql);
        StringBuffer messageBuf = new StringBuffer();
        while (matcher.find()){
            String paramKey = matcher.group();
            String paramValue = replaceParam.getString(paramKey.substring(2, paramKey.length()-1));
            matcher.appendReplacement(messageBuf, paramValue);
        }
        matcher.appendTail(messageBuf);
        return messageBuf.toString();
    }

    /**
     * 唯一id
     * @return
     */
    public static String unionId() {
        String uuid = UUID.randomUUID().toString();
        return uuid.substring(0, 12);
    }

    public static void main(String[] args) {
        System.out.println(unionId());
        System.out.println(unionId());
        System.out.println(unionId());
        System.out.println(unionId());
        System.out.println(unionId());
        System.out.println(unionId());
        System.out.println(unionId());

    }
}
